package io.github.wx.service;

import com.jfinal.kit.JMap;
import com.jfinal.plugin.activerecord.SqlPara;
import com.xiaoleilu.hutool.util.CollectionUtil;
import io.github.wx.common.constant.EnumFuctionStatus;
import io.github.wx.common.easyui.TreeNode;
import io.github.wx.dao.model.SysMenu;
import io.github.wx.dao.model.SysUser;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jie on 2017/4/8.
 * 菜单管理
 */
public class MenuService {
    /**
     * 获取全部的菜单并按照树结构展示
     *
     * @return List<TreeNode>
     */
    public List<TreeNode> selectAll() {
        List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
        // 查询所有菜单
        String sql = "SELECT * FROM sys_menu WHERE del_flag = ? ORDER BY sort ASC ";
        List<SysMenu> menuList = SysMenu.dao.find(sql, EnumFuctionStatus.NORMAL_STATUS.getCode());
        if (CollectionUtil.isEmpty(menuList)) {
            return null;
        }
        for (SysMenu menu : menuList) {
            TreeNode node = new TreeNode();
            node.setId(menu.getMenuId());
            node.setPid(menu.getParentId());
            node.setText(menu.getMenuName());
            node.setAttributes(menu.getUrl());
            node.setState(menu.getOpened());
            node.setPermission(menu.getPermission());
            node.setSort(menu.getSort());
            node.setStatus(menu.getStatus());
            node.setType(menu.getType());
            treeNodeList.add(node);
        }
        return treeNodeList;
    }

    /**
     * 根绝角色ID 获取菜单列表
     *
     * @param roleId 角色ID
     * @return SysMenuList
     */
    public List<SysMenu> findByRoleId(Integer roleId) {
        JMap params = new JMap();
        params.put("roleId", roleId);
        params.put("del_flag", EnumFuctionStatus.NORMAL_STATUS.getCode());
        SqlPara sqlPara = SysMenu.dao.getSqlPara("menu.findByRoleId", params);
        return SysMenu.dao.find(sqlPara);
    }
}

